<?php

namespace App\Admin\Controllers\Order;

use App\Admin\Forms\GoodsOrderRefunds;
use App\Models\Order\GoodsOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class GoodsOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(GoodsOrder::with(['user']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->showQuickEditButton('编辑');

            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('order_no');
            $grid->column('user.name', '订购人');
            $grid->column('coupon_code');
            $grid->column('location_type')->using(trans('goods-order.options.location_type'));
            $grid->column('remark');
            $grid->column('sum_price');
            $grid->column('discount');
            $grid->column('final_price');
            $grid->column('payment_status')->using(trans('goods-order.options.payment_status'));
            $grid->column('refund', '退款')->if(function () {
                return $this->payment_status == 2;
            })->display('退款')->modal(function ($modal) {
                $modal->title('退款');
                return GoodsOrderRefunds::make()->payload(['id' => $this->id]);
            });
//            $grid->column('payment_result');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->like('order_no')->width('300px');
                $filter->equal('location_type')->select(trans('goods-order.options.location_type'))->width('300px');
                $filter->equal('payment_status')->select(trans('goods-order.options.payment_status'))->width('300px');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GoodsOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_no');
            $show->field('user_id');
            $show->field('coupon_code');
            $show->field('remark');
            $show->field('sum_price');
            $show->field('discount');
            $show->field('final_price');
            $show->field('payment_status');
            $show->field('payment_result');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodsOrder(), function (Form $form) {
            $form->display('id');
            $form->display('order_no');
            $form->display('user.name', '订购人');
            $form->select('location_type')->options(trans('goods-order.options.location_type'));
            $form->select('payment_status')->options(trans('goods-order.options.payment_status'));
        });
    }
}
